using UnityEngine;
using System;
using System.Collections;
 
public class LightColor: MonoBehaviour {
	public bool debug			= false;
	public float dawnLength		=	6;
	public float dayLength		=	6;
	public float duskLength		=	6;
	public float nightLength	=	6;
	public float startingTime	=	0;
	//public Color clampColor     = Color.white;
	
	private Color currentAmbient	= Color.white;
	public Color ambientDawn		= Color.white;
	public Color ambientDay			= Color.white;
	public Color ambientDusk		= Color.white;
	public Color ambientNight		= Color.white;
	//public Color moonColor			= Color.white;
	
	public Color fogDawn			= Color.white;
	public Color fogDay				= Color.white;
	public Color fogDusk			= Color.white;
	public Color fogNight			= Color.white;
	private Color currentFog;
	
	public float shaftIntensityDawn	= 1;
	public float shaftIntensityDay	= 1;
	public float shaftIntensityDusk	= 1;
	public float shaftIntensityNight= 1;
	private float currentShaftIntensity;
	
    private float timeOfTheDay = 12.0f;
	public float timeSpeed = 1.0f;
	private GameObject mainCamera;
	private float resetTimer = 0;
 	
	private Skybox skybox;
	
	private float totalTime;
	
	
	private Color currentColor 	= Color.white;
	public Color colorDawn		=	Color.white;
	public Color colorDay		=	Color.white;
	public Color colorDusk		=	Color.white;
	public Color colorNight		=	Color.white;
	
	void Start(){

		totalTime = dawnLength + dayLength + duskLength + nightLength;	
		startingTime = Mathf.Clamp(startingTime,0,totalTime);
		//color = new Color( 0.9843f, 0.5098f, 0);
	}
	
	void Update () {
       timeOfTheDay = (startingTime + Time.time*timeSpeed - resetTimer)%totalTime;
		
		
       if ( this.GetComponent<Traverse>() != null)
	  	 this.GetComponent<Traverse>().each(delegate(GameObject obj) {
				
				Light lightObject = obj.light;
				if (lightObject != null) {
					updateSunLightColor(timeOfTheDay,lightObject);
				}
				return false;
			});
			
			
			
		
	}
 
    private void updateSunLightColor(float currentTime, Light currentLight)
    {
 
        float[] dColor = new float[4];
        dColor[0] = 0.9843f;
        dColor[1] = 0.5098f;
        dColor[2] = 0;
        dColor[3] = 1;
        // Create an array defines the new sunlight color.
        float[] newColor = new float[4];
        // If the current time is between 6:00 and 12:00, change the color towards white.
        if (currentTime >= dawnLength && currentTime < (dawnLength + dayLength))
        {
           /* for (int i = 0; i < newColor.Length; i++)
            {
                newColor[i] = dColor[i] + ((1 - dColor[i]) / dayLength) * (currentTime - dawnLength);
            }*/
			currentColor = Color.Lerp(colorDawn,colorDay,(currentTime-dawnLength)/dayLength);
			
			currentAmbient = Color.Lerp(ambientDawn,ambientDay,(currentTime-dawnLength)/dayLength);
			currentFog = Color.Lerp(fogDawn,fogDay,(currentTime-dawnLength)/dayLength);
			currentShaftIntensity = Mathf.Lerp(shaftIntensityDawn,shaftIntensityDay,(currentTime-dawnLength)/dayLength);
			
        }
        // Else if the current time is between 12:00 and 18:00, change the color towards dColor.
        else if (currentTime >= dawnLength + dayLength && currentTime < duskLength + dayLength + dawnLength)
        {
            for (int i = 0; i < newColor.Length; i++)
            {
                newColor[i] = 1 - ((1 - dColor[i]) / duskLength) * (currentTime - dawnLength - dayLength);
            }
			currentColor = Color.Lerp(colorDay,colorDusk,(currentTime-dawnLength - dayLength)/duskLength);
			currentAmbient = Color.Lerp(ambientDay,ambientDusk,(currentTime-dawnLength - dayLength)/duskLength);
			currentFog = Color.Lerp(fogDay,fogDusk,(currentTime-dawnLength - dayLength)/duskLength);
			currentShaftIntensity = Mathf.Lerp(shaftIntensityDay,shaftIntensityDusk,(currentTime-dawnLength - dayLength)/duskLength);
			if (skybox != null)
			skybox.material.SetFloat("_Blend",(currentTime-dawnLength - dayLength)/duskLength);
        }
        // Else if the current time is between 18:00 and 24:00, change the color towards black.
        else if (currentTime >= duskLength + dayLength + dawnLength && currentTime < totalTime)
        {
            for (int i = 0; i < newColor.Length; i++)
            {
                newColor[i] = dColor[i] - (dColor[i] / nightLength) * (currentTime - duskLength - dayLength - dawnLength);
            }
			currentColor = Color.Lerp(colorDusk,colorNight,(currentTime - duskLength - dayLength - dawnLength)/nightLength);
			currentAmbient = Color.Lerp(ambientDusk,ambientNight,(currentTime - duskLength - dayLength - dawnLength)/nightLength);
			currentFog = Color.Lerp(fogDusk,fogNight,(currentTime - duskLength - dayLength - dawnLength)/nightLength);
			currentShaftIntensity = Mathf.Lerp(shaftIntensityDusk,shaftIntensityNight,(currentTime - duskLength - dayLength - dawnLength)/nightLength);
        }
        // Else if the current time is between 0:00 and 6:00, change the color towards dColor.
        else if (currentTime >= 0 && currentTime < dawnLength)
        {
            for (int i = 0; i < newColor.Length; i++)
            {
                newColor[i] = (dColor[i] / dawnLength) * (currentTime - 0);
            }
			currentColor = Color.Lerp(colorNight,colorDawn,currentTime/dawnLength);
			currentAmbient = Color.Lerp(ambientNight,ambientDawn,currentTime/dawnLength);
			currentFog = Color.Lerp(fogNight,fogDawn,currentTime/dawnLength);
			currentShaftIntensity = Mathf.Lerp(shaftIntensityNight,shaftIntensityDawn,currentTime/dawnLength);
			if (skybox != null)
			skybox.material.SetFloat("_Blend",1-currentTime/dawnLength);	
        }
        // RenderSettings.fogColor = new Color(newColor[0], newColor[1], newColor[2], newColor[3]);
        currentLight.color = currentColor;//new Color(	Mathf.Clamp(newColor[0],0,clampColor.r), Mathf.Clamp(newColor[1],0,clampColor.g), Mathf.Clamp(newColor[2],0,clampColor.b), 1);//newColor[3]);
		//currentLight.color = Color.Lerp(moonColor,currentLight.color,newColor[3]);
		RenderSettings.ambientLight = currentAmbient;
		//if (currentLight.gameObject.GetComponent<LightScript>() != null)
		//	currentLight.intensity = newColor[3]*currentLight.GetComponent<LightScript>().getStartIntensity();
	/*	if (skybox != null){
			skybox.material.SetFloat("_Blend",1-newColor[3]);	
		}*/
		if (mainCamera != null){
			if (mainCamera.GetComponent<SunShafts>() != null){
				mainCamera.GetComponent<SunShafts>().scaleShafts(currentShaftIntensity);
				mainCamera.GetComponent<SunShafts>().sunColor = currentAmbient;
				mainCamera.GetComponent<SunShafts>().sunTransform = this.transform;	
			}
			if (mainCamera.GetComponent<GlobalFog>() != null)
				mainCamera.GetComponent<GlobalFog>().globalFogColor = currentFog;
			if (skybox == null)
					if (mainCamera.GetComponent<Skybox>() != null)
						skybox = mainCamera.GetComponent<Skybox>();
		}
		
    }
	
	
	void OnGUI(){
		if (debug)
			GUI.Label(new Rect(Screen.width/2-50,Screen.height-50,100,25),timeOfTheDay.ToString(),"box");	
	}
 
	public void setCamera(GameObject camera){
		mainCamera = camera;	
	}
	
	public void resetTime(){	
		resetTimer = Time.time*timeSpeed;
	}
 
}